// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/buffer/buffer.h>

namespace kumo::pollux::parquet {
    class NestedStructureDecoder {
    public:
        /// This function constructs the offsets, lengths and nulls arrays for the
        /// current level for complext types including ARRAY and MAP. The level is
        /// identified by the max definition level and repetition level for that
        /// level. For example, ARRAY<ARRAY<INTEGER>> has the following max definition
        /// and repetition levels:
        ///
        /// type    | ARRAY | ARRAY | INTEGER
        /// level   |   1   |   2   |   3
        /// maxDef  |   1   |   3   |   5
        /// maxRep  |   1   |   2   |   2
        ///
        /// If maxDefinition = 3 and maxRepeat = 2, this function will output
        /// offsets/lengths/nulls for the second level ARRAY.
        ///
        /// @param definitionLevels The definition levels for the leaf level
        /// @param repetitionLevels The repetition levels for the leaf level
        /// @param numValues The number of elements in definitionLevels or
        /// repetitionLevels
        /// @param maxDefinition The maximum possible definition level for this nested
        /// level
        /// @param maxRepeat The maximum possible repetition level for this nested
        /// level
        /// @param offsetsBuffer The output buffer for the offsets integer array.
        /// @param lengthsBuffer The output buffer for the lengths integer array. The
        /// elements are the number of elements in the designated level of collection.
        /// @param nullsBuffer The output buffer for the nulls bitmap.
        /// @return The number of elements for the current nested level.
        static int64_t readOffsetsAndNulls(
            const uint8_t *definitionLevels,
            const uint8_t *repetitionLevels,
            int64_t numValues,
            uint8_t maxDefinition,
            uint8_t maxRepeat,
            BufferPtr &offsetsBuffer,
            BufferPtr &lengthsBuffer,
            BufferPtr &nullsBuffer,
            memory::MemoryPool &pool);

    private:
        NestedStructureDecoder() {
        }
    };
} // namespace kumo::pollux::parquet
